package
{
	import code.StarlingMain;
	import code.ws_common_net.code.NetConnectManager;
	import code.ws_common_utils.SysUtils;
	
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemIdleMode;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.system.Capabilities;
	import flash.ui.Keyboard;
	
	import starling.core.Starling;

	/**
	 * 主文件 
	 * @author qiangchen
	 * 
	 */	
	[SWF(width=1280, height=752, backgroundColor=0, frameRate="30")]
	public class ws_mobile extends Sprite
	{
		/** logo embed resource */
		[Embed("assets/res/other/Default.jpg")]
		private static const logo:Class;
		private var _logo:Bitmap;
		
		/** 主类自身 */
		public static var _instance:ws_mobile;
		/** starling引擎 */
		public var _starling:Starling = null;
	
		/**
		 * 构造函数 
		 * 
		 */		
		public function ws_mobile():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
		
			this.addEventListener( Event.ADDED_TO_STAGE, addtostage_handler );
		}
		
		private function addtostage_handler( evt:Event ):void
		{
			NetConnectManager.getInstance;

			this.removeEventListener( Event.ADDED_TO_STAGE, addtostage_handler );
			// 初始化基本事件
			inis();
			// 初始化starling驱动器
			iniStarling();
		}
		
		
		private function inis():void
		{
			_instance = this;
			SysUtils.stage = stage;
			
			_logo = new logo() as Bitmap;
			addChild( _logo );
		}
		
		
		private function iniStarling():void
		{
//			Starling.handleLostContext = true;
			
			_starling = new Starling( StarlingMain, stage );
//			_starling.enableErrorChecking = true;
			_starling.antiAliasing = 1;
			_starling.start();
			
			if( Capabilities.cpuArchitecture == "ARM" )
			{
				NativeApplication.nativeApplication.addEventListener( Event.ACTIVATE, activate_Handler );
				NativeApplication.nativeApplication.addEventListener( Event.DEACTIVATE, deactivate_handler );
				NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, keydown_handler );
			}
			
			// 帧频、内存状态
//			var state:Stats = new Stats();
//			addChild( state );
		}
		
		/**
		 * 返回键 
		 * @param evt
		 * 
		 */		
		private function keydown_handler( evt:KeyboardEvent ):void
		{
			if( evt.keyCode == Keyboard.BACK )
			{
				evt.preventDefault();
				trace("KEYDOWN");
				
				if( StarlingMain.instance && StarlingMain.instance.alert )
				{
					StarlingMain.instance.alert.openOrClose( true );
				}
			}
		}
		
		/**
		 * 获取焦点 
		 * @param evt
		 * 
		 */		
		private function activate_Handler( evt:flash.events.Event):void
		{
			NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
			trace("ACTIVATE");
//			_starling.start();
		}
		
		/**
		 * 丢失焦点 
		 * @param evt
		 * 
		 */		
		private function deactivate_handler( evt:flash.events.Event ):void
		{
			NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
			trace("DEACTIVATE");
//			_starling.stop();
			NativeApplication.nativeApplication.exit();
		}
		
		
		/**
		 * 释放logo图 
		 * 
		 */		
		public function removeLogo():void
		{
			_logo.bitmapData.dispose();
			_logo.bitmapData = null;
			_logo.parent.removeChild( _logo );
			_logo = null;
		}
	}
}